~ chicken-core (chicken-5) /manual/C interface
Trap1[[tags: manual]]
2[[toc:]]
3
4
5== C interface
6
7The following functions and macros are available for C code that invokes
8Scheme or foreign procedures that are called by Scheme:
9
10=== Temporary stack
11
12==== C_save
13
14 [C macro] void C_save (C_word x) :
15
16Saves the Scheme data object {{x}} on the temporary stack.
17
18==== C_restore
19
20 [C macro] C_word C_restore
21
22Pops and returns the topmost value from the temporary stack.
23
24
25=== Type/value predicates
26
27When writing C code that accepts Scheme objects you often need to do
28checking what type of object is passed. These can help you determine
29the type of an object.
30
31==== C predicates
32
33These return regular C integer values (ie, zero is false, nonzero true).
34
35===== C_truep
36
37 [C macro] int C_truep(C_word x)
38
39Is {{x}} a truthy value, i.e. anything except {{C_SCHEME_FALSE}}?
40
41===== C_immediatep
42
43 [C macro] int C_immediatep(C_word x)
44
45Is {{x}} an immediate object?
46(see below for a definition)
47
48===== C_fitsinfixnump
49
50 [C macro] int C_fitsinfixnump(int number)
51
52Will {{number}} fit in a fixnum? It will fit when there is room for
53one additional type bit to tag it as a fixnum (assuming one bit is
54already used for the sign). In practice this means that the number's
55top two bits must be identical.
56
57===== C_ufitsinfixnump
58
59 [C macro] int C_ufitsinfixnump(unsigned int number)
60
61Like {{C_fitsinfixnump}} but for unsigned integers. This checks the
62top ''two'' bits are zero, since fixnums '''always''' carry a sign.
63
64
65==== Scheme predicates
66
67These return Scheme booleans (ie, C_SCHEME_TRUE or C_SCHEME_FALSE).
68This means they can be used directly from Scheme using {{##core#inline}}.
69
70===== C_zero_length_p
71
72 [C macro] C_word C_zero_length_p(C_word x)
73
74Is {{x}} a Scheme object of zero length? Only accepts non-immediate objects.
75
76===== C_unboundvaluep
77
78 [C macro] C_word C_unboundvaluep(C_word x)
79
80Is {{x}} the special unbound variable placeholder {{C_SCHEME_UNBOUND}}?
81
82===== C_boundp
83
84 [C macro] C_word C_boundp(C_word x)
85
86Is {{x}} a bound value? Only accepts non-immediate objects.
87
88===== C_blockp
89
90 [C macro] C_word C_blockp(C_word x)
91
92Is {{x}} a "block" value?
93
94A "block" value is a value that contains a memory block, i.e. is not
95an immediate value.
96
97===== C_immp
98
99 [C macro] C_word C_immp(C_word x)
100
101Is {{x}} an immediate value?
102
103===== C_forwardedp
104
105 [C macro] C_word C_forwardedp(C_word x)
106
107Is {{x}} a GC-forwarded object?
108
109===== C_flonump
110
111 [C macro] C_word C_flonump(C_word x)
112
113Is {{x}} a Scheme flonum object? Accepts only non-immediate objects.
114
115===== C_stringp
116
117 [C macro] C_word C_stringp(C_word x)
118
119Is {{x}} a Scheme string object? Accepts only non-immediate objects.
120
121===== C_symbolp
122
123 [C macro] C_word C_symbolp(C_word x)
124
125Is {{x}} a symbol? Accepts only non-immediate objects.
126
127===== C_pairp
128
129 [C macro] C_word C_pairp(C_word x)
130
131Is {{x}} a pair? Accepts only non-immediate objects.
132
133===== C_closurep
134
135 [C macro] C_word C_closurep(C_word x)
136
137Is {{x}} a closure? Accepts only non-immediate objects.
138
139===== C_vectorp
140
141 [C macro] C_word C_vectorp(C_word x)
142
143Is {{x}} any kind of vector? Accepts only non-immediate objects.
144
145This returns true for both regular heterogenous R5RS vectors and
146bytevectors ("blobs"). However, it does ''not'' return true for
147SRFI-4 vectors, as those are actually bytevectors wrapped in a
148structure with a type tag.
149
150===== C_bytevectorp
151
152 [C macro] C_word C_bytevectorp(C_word x)
153
154Is {{x}} a bytevector ("blob")? Accepts only non-immediate objects.
155
156===== C_portp
157
158 [C macro] C_word C_portp(C_word x)
159
160Is {{x}} a port object? Accepts only non-immediate objects.
161
162===== C_structurep
163
164 [C macro] C_word C_structurep(C_word x)
165
166Is {{x}} a structure (record) object? Accepts only non-immediate objects.
167
168===== C_locativep
169
170 [C macro] C_word C_locativep(C_word x)
171
172Is {{x}} a locative object? Accepts only non-immediate objects.
173
174===== C_charp
175
176 [C macro] C_word C_charp(C_word x)
177
178Is {{x}} a character object?
179
180===== C_booleanp
181
182 [C macro] C_word C_booleanp(C_word x)
183
184Is {{x}} a boolean object?
185
186===== C_eofp
187
188 [C macro] C_word C_eofp(C_word x)
189
190Is {{x}} the {{#!eof}} object?
191
192===== C_undefinedp
193
194 [C macro] C_word C_undefinedp(C_word x)
195
196Is {{x}} the undefined value?
197
198===== C_fixnump
199
200 [C macro] C_word C_fixnump(C_word x)
201
202Is {{x}} a fixnum object?
203
204===== C_nfixnump
205
206 [C macro] C_word C_nfixnump(C_word x)
207
208Is {{x}} ''not'' a fixnum object?
209
210===== C_bignump
211
212 [C macro] C_word C_bignump(C_word x)
213
214Is {{x}} a Scheme bignum object? Accepts only non-immediate objects.
215
216===== C_i_numberp
217
218 [C function] C_word C_i_numberp(C_word x)
219
220Is {{x}} a number object (fixnum, bignum, flonum, ratnum, cplxnum)?
221
222===== C_i_bignump
223
224 [C function] C_word C_i_bignump(C_word x)
225
226Is {{x}} a Scheme bignum object?
227
228===== C_i_cplxnump
229
230 [C function] C_word C_i_cplxnump(C_word x)
231
232Is {{x}} a Scheme cplxnum object?
233
234===== C_i_ratnump
235
236 [C function] C_word C_i_ratnump(C_word x)
237
238Is {{x}} a Scheme ratnum object?
239
240===== C_i_flonump
241
242 [C function] C_word C_i_flonump(C_word x)
243
244Is {{x}} a flonum object?
245
246===== C_i_exact_integerp
247
248 [C macro] C_word C_i_exact_integerp(C_word x)
249
250Is {{x}} an exact integer (i.e., a fixnum or a bignum)?
251
252===== C_pointerp
253
254 [C macro] C_word C_pointerp(C_word x)
255
256Is {{x}} a C pointer object? Only accepts non-immediate objects.
257
258===== C_taggedpointerp
259
260 [C macro] C_word C_taggedpointerp(C_word x)
261
262Is {{x}} a tagged pointer object? Only accepts non-immediate objects.
263
264===== C_anypointerp
265
266 [C macro] C_word C_anypointerp(C_word x)
267
268Is {{x}} any type of pointer object? Only accepts non-immediate objects.
269
270===== C_lambdainfop
271
272 [C macro] C_word C_lambdainfop(C_word x)
273
274Is {{x}} a lambda-info object? Only accepts non-immediate objects.
275
276===== C_byteblockp
277
278 [C macro] C_word C_byteblockp(C_word x)
279
280Is {{x}} a "byteblock" object? Only accepts non-immediate objects.
281
282Strings, flonums, bytevectors and lambda-info objects are considered
283"byteblock" objects, as they are not containers for Scheme objects but
284simply point to contiguous memory ranges of bytes.
285
286===== C_specialp
287
288 [C macro] C_word C_specialp(C_word x)
289
290Is {{x}} a "special" object? Only accepts non-immediate objects.
291
292Closures, ports, pointers and locatives are considered "special"
293objects, as they are not containers for Scheme objects (and they are
294not byte blocks either), so they have to be treated specially by the GC.
295
296===== C_nullp
297
298 [C macro] C_word C_nullp(C_word x)
299
300Is {{x}} the empty list, i.e. is it {{C_SCHEME_END_OF_LIST}}?
301
302===== C_anyp
303
304 [C macro] C_word C_anyp(C_word x)
305
306Always returns {{C_SCHEME_TRUE}}.
307
308
309=== Constructors
310
311==== Constructors for immediate Scheme objects
312
313"immediate" Scheme objects are objects that are represented directly
314by a {{C_word}}. There's no additional memory used by them.
315
316===== C_fix
317
318 [C macro] C_word C_fix (int integer)
319
320===== C_make_character
321
322 [C macro] C_word C_make_character (int char_code)
323
324===== C_mk_bool
325
326 [C macro] C_word C_mk_bool(int truth_value)
327
328===== C_mk_nbool
329
330 [C macro] C_word C_mk_nbool(int truth_value_to_negate)
331
332===== C_SCHEME_END_OF_LIST
333
334 [C macro] C_SCHEME_END_OF_LIST
335
336===== C_SCHEME_END_OF_FILE
337
338 [C macro] C_SCHEME_END_OF_FILE
339
340===== C_SCHEME_FALSE
341
342 [C macro] C_SCHEME_FALSE
343
344===== C_SCHEME_TRUE
345
346 [C macro] C_SCHEME_TRUE
347
348
349==== Constructors for non-immediate Scheme objects
350
351Non-immediate Scheme objects are still represented and passed around
352by a single {{C_word}}, but this is basically just a pointer to the
353start of the object (which should never be treated as such, use the
354accessor macros instead).
355
356===== C_string
357
358 [C function] C_word C_string (C_word **ptr, int length, char *string)
359
360===== C_string2
361
362 [C function] C_word C_string2 (C_word **ptr, char *zero_terminated_string)
363
364===== C_intern
365
366 [C function] C_word C_intern (C_word **ptr, int length, char *string)
367
368===== C_intern2
369
370 [C function] C_word C_intern2 (C_word **ptr, char *zero_terminated_string)
371
372===== C_intern3
373
374 [C function] C_word C_intern3 (C_word **ptr, char *zero_terminated_string, C_word initial_value)
375
376===== C_a_pair
377
378 [C function] C_word C_a_pair (C_word **ptr, C_word car, C_word cdr)
379
380===== C_flonum
381
382 [C function] C_word C_flonum (C_word **ptr, double number)
383
384===== C_int_to_num
385
386 [C function] C_word C_int_to_num (C_word **ptr, int integer)
387
388===== C_mpointer
389
390 [C function] C_word C_mpointer (C_word **ptr, void *pointer)
391
392===== C_vector
393
394 [C function] C_word C_vector (C_word **ptr, int length, ...)
395
396===== C_bytevector
397
398 [C function] C_word C_bytevector (C_word **ptr, int length, C_char *data)
399
400===== C_structure
401
402 [C function] C_word C_structure (C_word **ptr, int length, ...)
403
404===== C_list
405
406 [C function] C_word C_list (C_word **ptr, int length, ...)
407
408===== C_closure
409
410 [C function] C_word C_closure (C_word **ptr, int length, C_word procedure, ...)
411
412These functions allocate memory from {{ptr}} and initialize a fresh
413data object. The new data object is returned. {{ptr}} should be the
414'''address''' of an allocation pointer created with {{C_alloc}}.
415
416To find out how big the memory block should be, use the {{C_SIZEOF_*}}
417macros described below.
418
419Here's an example how to create a closure that accepts a vector,
420stores the Scheme numbers 1, 2 and 3 and a given string in it and
421returns that vector to its continuation:
422
423<enscript highlight=scheme>
424#>
425
426#include <assert.h>
427
428void fill_vector(C_word c, C_word *av)
429{
430 C_word closure = av[0];
431 C_word kontinuation = C_block_item(closure, 1);
432 C_word vec = av[1];
433
434 C_block_item(vec, 0) = C_fix(1);
435 C_block_item(vec, 1) = C_fix(2);
436 C_block_item(vec, 2) = C_fix(3);
437 C_block_item(vec, 3) = C_block_item(closure, 2);
438
439 C_kontinue(kontinuation, vec);
440}
441
442void one_two_three(C_word continuation, C_word str)
443{
444 /*
445 * Allocate room on the stack to hold the closure: 1 word for
446 * the type tag, 1 word for the procedure and 2 words for the
447 * values "closed over"; this procedure's continuation "k" and
448 * the argument "str". We could also use C_alloc(4).
449 */
450 C_word closure[4], *cp = closure;
451
452 /* Allocate room for the argvector for C_allocate_vector */
453 C_word av[6];
454
455 /* Create the closure. It holds 3 values, not counting the tag */
456 C_word closure_object = C_closure(&cp, 3, (C_word)fill_vector, continuation, str);
457
458 /*
459 * After this, cp points just beyond the last word of the allocated
460 * data and closure_object is an opaque representation of the newly
461 * created closure as a whole, i.e. the following relations hold:
462 */
463 assert( (closure + 4) == cp );
464 assert( C_block_header(closure_object) == (*closure) );
465 assert( C_data_pointer(closure_object) == (closure + 1) );
466 assert( C_block_item(closure_object, 0) == (*(closure + 1)) );
467
468 /* Set up the arguments for C_allocate_vector */
469 av[0] = (C_word)NULL; /* Closure of allocate_vector - unused, so pass NULL */
470 av[1] = closure_object; /* Continuation to call after allocating Scheme vector */
471 av[2] = C_fix(4); /* Size of Scheme vector to allocate */
472 av[3] = C_SCHEME_FALSE; /* We want a regular vector, not a bytevector */
473 av[4] = C_SCHEME_FALSE; /* Initialization value for slots. Don't care */
474 av[5] = C_SCHEME_FALSE; /* Do not align at 8 byte (64-bit word) boundary */
475 /* Make a vector of 4 objects and use closure_object as continuation */
476 C_allocate_vector(6, av);
477 /* .. C_allocate_vector does not return ... */
478}
479<#
480
481
482(define one-two-three
483 (foreign-primitive ((scheme-object str)) "one_two_three(C_k, str);"))
484
485(print (one-two-three "hi"))
486</enscript>
487
488This is equivalent to the following in Scheme:
489
490<enscript highlight=scheme>
491(define (one-two-three str)
492 (let ((fill-vector (lambda (vec)
493 (vector-set! vec 0 1)
494 (vector-set! vec 1 2)
495 (vector-set! vec 2 3)
496 (vector-set! vec 3 str)
497 vec)))
498 (fill-vector (make-vector 4 #f))))
499
500(print (one-two-three "hi"))
501</enscript>
502
503
504==== Memory allocation
505
506These can be used to allocate memory for non-immediate objects.
507
508===== C_alloc
509
510 [C macro] C_word* C_alloc (int words)
511
512Allocates memory from the C stack ({{C_alloc}}) and returns a pointer to
513it. {{words}} should be the number of words needed for all data
514objects that are to be created in this function. Note that stack-allocated
515data objects have to be passed to Scheme callback functions, or they will
516not be seen by the garbage collector. This is really only usable for
517callback procedure invocations, make sure not to use it in normal code,
518because the allocated memory will be re-used after the foreign procedure
519returns. When invoking Scheme callback procedures a minor garbage
520collection is performed, so data allocated with {{C_alloc}}
521will already have moved to a safe place.
522
523Note that {{C_alloc}} is really just a wrapper around {{alloca}},
524and can also be simulated by declaring a stack-allocated array of
525{{C_word}}s:
526
527
528===== C_SIZEOF_LIST
529
530 [C macro] int C_SIZEOF_LIST (int length)
531
532Returns the size in words needed for allocation of a list with ''length'' elements.
533
534===== C_SIZEOF_STRING
535
536 [C macro] int C_SIZEOF_STRING (int length)
537
538Returns the size in words needed for allocation of a string with ''length'' characters.
539
540===== C_SIZEOF_BYTEVECTOR
541
542 [C macro] int C_SIZEOF_BYTEVECTOR (int length)
543
544Returns the size in words needed for allocation of a bytevector with ''length'' bytes of data.
545
546===== C_SIZEOF_VECTOR
547
548 [C macro] int C_SIZEOF_VECTOR (int length)
549
550Returns the size in words needed for allocation of vector with ''length'' elements.
551
552===== C_SIZEOF_CLOSURE
553
554 [C macro] int C_SIZEOF_CLOSURE (int length)
555
556Returns the size in words needed for allocation of a closure with {{length}} slots. The C function pointer also counts as a slot, so always remember to include it when calculating {{length}}.
557
558===== C_SIZEOF_STRUCT
559
560 [C macro] int C_SIZEOF_STRUCT (int length)
561
562Returns the size in words needed for allocation of a structure (record type) object with {{length}} slots. The structure's type tag also counts as a slot, so always remember to include it when calculating {{length}}.
563
564===== C_SIZEOF_BIGNUM
565
566 [C macro] int C_SIZEOF_BIGNUM (int length)
567
568Returns the size in words needed for allocation of a bignum object with {{length}} word-sized digits (limbs).
569
570===== C_SIZEOF_FIX_BIGNUM
571
572 [C macro] int C_SIZEOF_FIX_BIGNUM
573
574The size in words needed for allocation of a bignum object which is large enough to store any fixnum (ie, if it were converted to a denormalized bignum, because if a number ''can'' be represented as a fixnum, it ''will'' be).
575
576===== C_SIZEOF_INTERNED_SYMBOL
577
578 [C macro] int C_SIZEOF_INTERNED_SYMBOL (int length)
579
580===== C_SIZEOF_PAIR
581
582 [C macro] int C_SIZEOF_PAIR
583
584===== C_SIZEOF_FLONUM
585
586 [C macro] int C_SIZEOF_FLONUM
587
588===== C_SIZEOF_POINTER
589
590 [C macro] int C_SIZEOF_POINTER
591
592===== C_SIZEOF_LOCATIVE
593
594 [C macro] int C_SIZEOF_LOCATIVE
595
596===== C_SIZEOF_TAGGED_POINTER
597
598 [C macro] int C_SIZEOF_TAGGED_POINTER
599
600These are macros that return the size in words needed for a data object
601of a given type.
602
603=== Accessors
604
605==== C_character_code
606
607 [C macro] int C_character_code (C_word character)
608
609==== C_unfix
610
611 [C macro] int C_unfix (C_word fixnum)
612
613==== C_flonum_magnitude
614
615 [C macro] double C_flonum_magnitude (C_word flonum)
616
617==== C_c_string
618
619 [C function] char* C_c_string (C_word string)
620
621==== C_num_to_int
622
623 [C function] int C_num_to_int (C_word fixnum_or_bignum)
624
625==== C_pointer_address
626
627 [C function] void* C_pointer_address (C_word pointer)
628
629These macros and functions can be used to convert Scheme data objects
630back to C data. Note that {{C_c_string()}} returns a pointer
631to the character buffer of the actual Scheme object and is not
632zero-terminated.
633
634==== C_header_size
635
636 [C macro] int C_header_size (C_word x)
637
638==== C_header_bits
639
640 [C macro] int C_header_bits (C_word x)
641
642Return the number of elements and the type-bits of the non-immediate
643Scheme data object {{x}}.
644
645
646==== C_block_item
647
648 [C macro] C_word C_block_item (C_word x, int index)
649
650This macro can be used to access slots of the non-immediate Scheme data
651object {{x}}. {{index}} specifies the index of the slot to
652be fetched, starting at 0. Pairs have 2 slots, one for the '''car'''
653and one for the '''cdr'''. Vectors have one slot for each element.
654
655
656==== C_u_i_car
657
658 [C macro] C_word C_u_i_car (C_word x)
659
660==== C_u_i_cdr
661
662 [C macro] C_word C_u_i_cdr (C_word x)
663
664Aliases for {{C_block_item(x, 0)}} and {{C_block_item(x, 1)}}, respectively.
665
666==== C_port_file
667
668 [C macro] C_word C_port_file (C_word x)
669
670Alias for {{(FILE *)C_block_item(x, 0)}}. To be used with port
671objects representing files (but will not work on sockets, for example).
672
673
674==== C_data_pointer
675
676 [C macro] void* C_data_pointer (C_word x)
677
678Returns a pointer to the data-section of a non-immediate Scheme object.
679
680
681=== C_make_header
682
683 [C macro] C_word C_make_header (C_word bits, C_word size)
684
685A macro to build a Scheme object header from its bits and size parts.
686
687
688=== C_mutate
689
690 [C function] C_word C_mutate (C_word *slot, C_word val)
691
692Assign the Scheme value {{val}} to the location specified by
693{{slot}}. If the value points to data inside the nursery (the first
694heap-generation), then the garbage collector will remember to handle the
695data appropriately. Assigning nursery-pointers directly will otherwise
696result in lost data. Note that no copying takes place at the moment
697when {{C_mutate}} is called, but later - at the next (minor) garbage
698collection.
699
700
701=== C_symbol_value
702
703 [C macro] C_word C_symbol_value (C_word symbol)
704
705Returns the global value of the variable with the name {{symbol}}. If the
706variable is unbound {{C_SCHEME_UNBOUND}} is returned. You can set a variable's
707value with {{C_mutate(&C_symbol_value(SYMBOL), VALUE)}}.
708
709
710=== GC interface
711
712==== C_gc_protect
713
714 [C function] void C_gc_protect (C_word *ptrs[], int n)
715
716Registers {{n}} variables at address {{ptrs}} to be garbage collection roots.
717The locations should not contain pointers to data allocated in the nursery, only
718immediate values or pointers to heap-data are valid. Any
719assignment of potential nursery data into a root-array should be done
720via {{C_mutate()}}. The variables have to be initialized to sensible values
721before the next garbage collection starts (when in doubt, set all locations
722in {{ptrs}} to {{C_SCHEME_UNDEFINED}})
723{{C_gc_protect}} may not called before the runtime system has been
724initialized (either by {{CHICKEN_initialize}}, {{CHICKEN_run}} or
725{{CHICKEN_invoke}}.
726
727For a slightly simpler interface to creating and using GC roots see
728{{CHICKEN_new_gc_root}}.
729
730
731==== C_gc_unprotect
732
733 [C function] void C_gc_unprotect (int n)
734
735Removes the last {{n}} registered variables from the set of
736root variables.
737
738
739==== C_pre_gc_hook
740
741 [C Variable] void (*C_pre_gc_hook)(int mode)
742
743If not {{NULL}}, the function pointed to by this variable will be
744called before each garbage collection with a flag indicating what kind
745of collection was performed (either {{0}} for a minor or major
746collection or {{2}} for a resizing collection). A "resizing"
747collection means a secondary collection that moves all live data into
748a enlarged (or shrinked) heap-space. Minor collections happen very
749frequently, so the hook function should not consume too much time. The
750hook function may not invoke Scheme callbacks.
751
752Note that resizing collections may be nested in normal major collections.
753
754==== C_post_gc_hook
755
756 [C Variable] void (*C_post_gc_hook)(int mode, long ms)
757
758If not {{NULL}}, the function pointed to by this variable will be
759called after each garbage collection with a flag indicating what kind
760of collection was performed (either {{0}} for a minor collection,
761{{1}} for a major collection or {{2}} for a resizing
762collection). Minor collections happen very frequently, so the hook
763function should not consume too much time. The hook function may not
764invoke Scheme callbacks. The {{ms}} argument records the number of
765milliseconds required for the garbage collection, if the collection
766was a major one. For minor collections the value of the {{ms}} argument
767is undefined.
768
769=== Type-specific macros and functions
770
771The following are macros and functions to ask information or perform
772operations on objects once their types are already known. If you call
773it on any object of another type, it is not defined what will happen
774and likely your program will crash, especially if you pass immediates
775to procedures expecting non-immediates.
776
777==== Vectors
778
779===== C_vemptyp
780
781 [C macro] C_word C_vemptyp(C_word v)
782
783Is the (byte- or heterogenous) vector {{v}} empty?
784
785===== C_notvemptyp
786
787 [C macro] C_word C_notvemptyp(C_word v)
788
789Is the (byte- or heterogenous) vector {{v}} nonempty?
790
791==== Numbers
792
793These procedures accept any type of number, so you can pass in a
794fixnum, a flonum, a bignum, a ratnum or a cplxnum. You shouldn't pass
795in another type though, since that could crash your program.
796
797===== C_u_i_exactp
798
799 [C macro] C_word C_u_i_exactp(C_word x)
800
801Is {{x}} an exact number (i.e., a fixnum, bignum, ratnum or exact cplxnum)?
802
803===== C_u_i_inexactp
804
805 [C macro] C_word C_u_i_inexactp(C_word x)
806
807Is {{x}} an inexact number (i.e., a flonum or an inexact cplxnum)?
808
809===== C_i_finitep
810
811 [C function] C_word C_i_finitep(C_word x)
812
813Is {{x}} a finite number? This returns false only when {{x}} is a
814flonum representing {{-inf}} or {{+inf}}.
815
816==== Bignums
817
818===== C_bignum_negativep
819
820 [C macro] int C_bignum_negativep(C_word b)
821
822Returns nonzero if the bignum {{b}} is negative, zero if it is not.
823
824===== C_bignum_digits
825
826 [C macro] C_uword *C_bignum_digits(C_word b)
827
828Returns a pointer to the first digit (the least significant one) of the bignum {{b}}.
829
830===== C_bignum_size
831
832 [C macro] C_word C_bignum_size(b)
833
834Returns the number of digits in the bignum {{b}}, as an unboxed C number. If you want a fixnum, use {{C_u_i_bignum_size}}.
835
836===== C_u_i_bignum_size
837
838 [C macro] C_word C_u_i_bignum_size(b)
839
840Returns the number of digits in the bignum {{b}}, as a Scheme fixnum. If you want an unboxed integer, use {{C_bignum_size}}.
841
842===== C_i_bignum_cmp
843
844 [C macro] C_word C_i_bignum_cmp(x, y)
845
846Compares the bignums {{x}} and {{y}} and returns the fixnums {{-1}}, {{0}} or {{1}} if {{x}} is less than, equal to or greater than {{y}}, respectively.
847
848==== Fixnums
849
850Note: Fixnums are immediates, so there is no {{C_fixnum_equalp}}
851macro. You can just compare them without hassle (or use
852[[#c-eqp|C_eqp]] if you prefer).
853
854===== C_i_fixnumevenp
855
856 [C macro] C_word C_i_fixnumevenp(C_word x)
857
858Is {{x}} an even fixnum?
859
860===== C_i_fixnumoddp
861
862 [C macro] C_word C_i_fixnumoddp(C_word x)
863
864Is {{x}} an odd fixnum?
865
866===== C_fixnum_times
867
868 [C macro] C_word C_fixnum_times(C_word n1, C_word n2)
869
870Multiply fixnum n1 by fixnum n2. Will not overflow into a bignum, but
871will handle overflows safely in the sense that it always produces a
872fixnum.
873
874===== C_a_i_fixnum_times
875
876 [C macro] C_word C_a_i_fixnum_times(C_word **ptr, C_word n, C_word x, C_word y)
877
878Calculate {{x}} * {{y}}, safely overflowing into a bignum, using the
879storage in {{ptr}} (which should be at least {{C_SIZEOF_BIGNUM(2)}}).
880
881
882===== C_fixnum_plus
883
884 [C macro] C_word C_fixnum_plus(C_word n1, C_word n2)
885
886Add fixnum {{n1}} to fixnum {{n2}}. Will not overflow into a bignum,
887but will handle overflows safely in the sense that it always produces
888a fixnum.
889
890===== C_u_fixnum_plus
891
892 [C macro] C_word C_u_fixnum_plus(C_word n1, C_word n2)
893
894Like {{C_fixnum_plus}}, but unsafe (assumes no overflow/underflow).
895
896===== C_a_i_fixnum_plus
897
898 [C macro] C_word C_a_i_fixnum_plus(C_word **ptr, C_word n, C_word x, C_word y)
899
900Calculate {{x}} + {{y}}, safely overflowing into a bignum, using the
901storage in {{ptr}} (which should be at least {{C_SIZEOF_FIX_BIGNUM}}).
902
903===== C_fixnum_difference
904
905 [C macro] C_word C_fixnum_difference(C_word n1, C_word n2)
906
907Calculate {{n1}} - {{n2}}. Will not overflow into a bignum, but will
908handle overflows safely in the sense that it always produces a fixnum.
909
910===== C_u_fixnum_difference
911
912 [C macro] C_word C_u_fixnum_difference(C_word n1, C_word n2)
913
914Like {{C_fixnum_difference}}, but unsafe (assumes no overflow/underflow).
915
916===== C_a_i_fixnum_difference
917
918 [C macro] C_word C_a_i_fixnum_difference(C_word **ptr, C_word n, C_word x, C_word y)
919
920Calculate {{x}} - {{y}}, safely overflowing into a bignum, using the
921storage in {{ptr}} (which should be at least {{C_SIZEOF_FIX_BIGNUM}}).
922
923
924===== C_fixnum_divide
925===== C_u_fixnum_divide
926
927 [C macro] C_word C_fixnum_divide(C_word n1, C_word n2)
928 [C macro] C_word C_u_fixnum_divide(C_word n1, C_word n2)
929
930Divide {{n1}} by {{n2}}, returning the quotient (i.e., integer
931division). {{C_fixnum_divide}} signals an error if {{n2}} is zero.
932
933===== C_fixnum_modulo
934===== C_u_fixnum_modulo
935
936 [C macro] C_word C_fixnum_modulo(C_word n1, C_word n2)
937 [C macro] C_word C_u_fixnum_modulo(C_word n1, C_word n2)
938
939Calculate {{n1}} modulo {{n2}}. {{C_fixnum_modulo}} signals an error if {{n2}} is zero. Neither handles overflow into bignums.
940
941===== C_a_i_fixnum_quotient_checked
942
943 [C macro] C_word C_a_i_fixnum_quotient_checked(C_word **ptr, C_word n, C_word x, C_word y)
944
945Calculate integer division of {{x}} / {{y}}, safely overflowing into a
946bignum (which can happen when dividing by {{C_MOST_NEGATIVE_FIXNUM}}),
947using the storage in {{ptr}} (which should be at least
948{{C_SIZEOF_FIX_BIGNUM}}). If {{y}} is zero, this will signal an
949error.
950
951===== C_i_fixnum_remainder_checked
952
953 [C macro] C_word C_i_fixnum_remainder_checked(C_word x, C_word y)
954
955Calculate the remainder of integer division {{x}} / {{y}}. If {{y}}
956is zero, this will signal an error.
957
958
959===== C_fixnum_and
960
961 [C macro] C_word C_fixnum_and(C_word n1, C_word n2)
962
963Calculate the bitwise {{AND}} of the integral values of {{n1}} and {{n2}}.
964
965===== C_u_fixnum_and
966
967 [C macro] C_word C_u_fixnum_and(C_word n1, C_word n2)
968
969Like {{C_fixnum_and}}, but unsafe.
970
971===== C_fixnum_or
972
973 [C macro] C_word C_fixnum_or(C_word n1, C_word n2)
974
975Calculate the bitwise {{OR}} of the integral values of {{n1}} and {{n2}}.
976
977===== C_u_fixnum_or
978
979 [C macro] C_word C_u_fixnum_or(C_word n1, C_word n2)
980
981Like {{C_fixnum_or}}, but unsafe.
982
983===== C_fixnum_xor
984
985 [C macro] C_word C_fixnum_xor(C_word n1, C_word n2)
986
987Calculate the bitwise {{XOR}} of the integral values of {{n1}} and {{n2}}.
988
989===== C_fixnum_not
990
991 [C macro] C_word C_fixnum_not(C_word n)
992
993Calculate the bitwise {{NOT}} (inversion of bits) of the integral
994value of {{n}}.
995
996===== C_fixnum_shift_left
997
998 [C macro] C_word C_fixnum_shift_left(C_word n1, C_word n2)
999
1000Shift the integral value of {{n1}} left by {{n2}} positions.
1001
1002===== C_fixnum_shift_right
1003
1004 [C macro] C_word C_fixnum_shift_right(C_word n1, C_word n2)
1005
1006Shift the integral value of {{n1}} right by {{n2}}
1007
1008===== C_fixnum_negate
1009
1010 [C macro] C_word C_fixnum_negate(C_word n)
1011
1012Negate {{n}}, i.e. return {{-n}}. This will ''not'' overflow into a
1013bignum.
1014
1015===== C_a_i_fixnum_negate
1016
1017 [C macro] C_word C_a_i_fixnum_negate(C_word **ptr, C_word n, C_word x)
1018
1019Negate {{n}}, i.e. return {{-n}}. This will not overflow into a
1020bignum, using the storage pointed to by {{ptr}}, which should at least
1021be {{C_SIZEOF_FIX_BIGNUM}}.
1022
1023
1024===== C_fixnum_greaterp
1025
1026 [C macro] C_word C_fixnum_greaterp(C_word n1, C_word n2)
1027
1028Returns {{C_SCHEME_TRUE}} when {{n1}} is greater than {{n2}},
1029{{C_SCHEME_FALSE}} if not.
1030
1031===== C_fixnum_greater_or_equal_p
1032
1033 [C macro] C_word C_fixnum_greater_or_equalp(C_word n1, C_word n2)
1034
1035Returns {{C_SCHEME_TRUE}} when {{n1}} is greater than or equal to
1036{{n2}}, {{C_SCHEME_FALSE}} if not.
1037
1038===== C_fixnum_lessp
1039
1040 [C macro] C_word C_fixnum_lessp(C_word n1, C_word n2)
1041
1042Returns {{C_SCHEME_TRUE}} when {{n1}} is less than {{n2}},
1043{{C_SCHEME_FALSE}} if not.
1044
1045===== C_fixnum_less_or_equal_p
1046
1047 [C macro] C_word C_fixnum_less_or_equalp(C_word n1, C_word n2)
1048
1049Returns {{C_SCHEME_TRUE}} when {{n1}} is less than or equal to
1050{{n2}}, {{C_SCHEME_FALSE}} if not.
1051
1052===== C_i_fixnum_positivep
1053
1054 [C macro] C_word C_i_fixnum_positivep(C_word n)
1055
1056Returns {{C_SCHEME_TRUE}} when {{n}} is a positive fixnum,
1057{{C_SCHEME_FALSE}} if it is zero or negative.
1058
1059===== C_i_fixnum_negativep
1060
1061 [C macro] C_word C_i_fixnum_negativep(C_word n)
1062
1063Returns {{C_SCHEME_TRUE}} when {{n}} is a negative fixnum,
1064{{C_SCHEME_FALSE}} if it is zero or positive.
1065
1066===== C_fixnum_increase
1067
1068 [C macro] C_word C_fixnum_increase(C_word n)
1069
1070Adds 1 to {{n}}
1071
1072===== C_u_fixnum_increase
1073
1074 [C macro] C_word C_u_fixnum_increase(C_word n)
1075
1076As {{C_fixnum_increase}}, but unsafe (assumes the result will not overflow).
1077
1078===== C_fixnum_decrease
1079
1080 [C macro] C_word C_fixnum_decrease(C_word n)
1081
1082Subtracts 1 from {{n}}
1083
1084===== C_u_fixnum_decrease
1085
1086 [C macro] C_word C_u_fixnum_decrease(C_word n)
1087
1088As {{C_fixnum_increase}}, but unsafe (assumes the result will not underflow).
1089
1090===== C_fixnum_abs
1091
1092 [C macro] C_word C_fixnum_abs(C_word n)
1093
1094Returns the absolute value of {{n}}.
1095
1096===== C_i_fixnum_min
1097
1098 [C function] C_word C_i_fixnum_min(C_word n1, C_word n2)
1099
1100Returns the smallest of the two fixnums {{n1}} and {{n2}}.
1101
1102===== C_i_fixnum_max
1103
1104 [C function] C_word C_i_fixnum_max(C_word n1, C_word n2)
1105
1106Returns the largest of the two fixnums {{n1}} and {{n2}}.
1107
1108===== C_i_fixnum_gcd
1109
1110 [C function] C_word C_i_fixnum_gcd(C_word n1, C_word n2)
1111
1112Returns the greatest common divisor of the two fixnums {{n1}} and {{n2}}.
1113
1114===== C_i_fixnum_length
1115
1116 [C function] C_word C_i_fixnum_length(C_word x)
1117
1118Returns the integer length in bits of the fixnum {{x}} (as a fixnum).
1119
1120
1121==== Flonums
1122
1123===== C_flonum_equalp
1124
1125 [C macro] C_word C_flonum_equalp(C_word n1, C_word n2)
1126
1127Returns {{C_SCHEME_TRUE}} when {{n1}} and {{n2}} are equal flonums,
1128{{C_SCHEME_FALSE}} otherwise.
1129
1130===== C_flonum_greaterp
1131
1132 [C macro] C_word C_flonum_greaterp(C_word n1, C_word n2)
1133
1134Returns {{C_SCHEME_TRUE}} when {{n1}} is greater than {{n2}},
1135{{C_SCHEME_FALSE}} if not.
1136
1137===== C_flonum_greater_or_equal_p
1138
1139 [C macro] C_word C_flonum_greater_or_equal_p(C_word n1, C_word n2)
1140
1141Returns {{C_SCHEME_TRUE}} when {{n1}} is greater than or equal to {{n2}},
1142{{C_SCHEME_FALSE}} if not.
1143
1144===== C_flonum_lessp
1145
1146 [C macro] C_word C_flonum_lessp(C_word n1, C_word n2)
1147
1148Returns {{C_SCHEME_TRUE}} when {{n1}} is less than {{n2}},
1149{{C_SCHEME_FALSE}} if not.
1150
1151===== C_flonum_less_or_equal_p
1152
1153 [C macro] C_word C_flonum_less_or_equal_p(C_word n1, C_word n2)
1154
1155Returns {{C_SCHEME_TRUE}} when {{n1}} is less than or equal to {{n2}},
1156{{C_SCHEME_FALSE}} if not.
1157
1158===== C_a_i_flonum_plus
1159
1160 [C macro] C_word C_a_i_flonum_plus(C_word **ptr, int c, C_word n1, C_word n2)
1161
1162Adds the flonum {{n1}} to the flonum {{n2}}, using the storage at
1163{{ptr}}. {{c}} should always be 2.
1164
1165Example:
1166
1167<enscript highlight=scheme>
1168#include <chicken.h>
1169#include <stdio.h>
1170
1171int main(void)
1172{
1173 C_word *mema, *memb, *memresult;
1174 C_word a, b, result;
1175
1176 mema = C_alloc(C_SIZEOF_FLONUM);
1177 memb = C_alloc(C_SIZEOF_FLONUM);
1178 memresult = C_alloc(C_SIZEOF_FLONUM);
1179 a = C_flonum(&mema, 1.2);
1180 b = C_flonum(&memb, 4.7);
1181 result = C_a_i_flonum_plus(&memresult, 2, a, b);
1182 printf("%lf\n", C_flonum_magnitude(result));
1183 return 0;
1184}
1185</enscript>
1186
1187This will print {{5.9}}
1188
1189===== C_a_i_flonum_difference
1190
1191 [C macro] C_word C_a_i_flonum_difference(C_word **ptr, int c, C_word n1, C_word n2)
1192
1193Subtracts the flonum {{n2}} from the flonum {{n1}}, using the storage at
1194{{ptr}}. {{c}} should always be 2.
1195
1196===== C_a_i_flonum_times
1197
1198 [C macro] C_word C_a_i_flonum_times(C_word **ptr, int c, C_word n1, C_word n2)
1199
1200Multiplies the flonum {{n1}} by the flonum {{n2}}, using the storage at
1201{{ptr}}. {{c}} should always be 2.
1202
1203===== C_a_i_flonum_quotient
1204
1205 [C macro] C_word C_a_i_flonum_quotient(C_word **ptr, int c, C_word n1, C_word n2)
1206 [C macro] C_word C_a_i_flonum_quotient_checked(C_word **ptr, int c, C_word n1, C_word n2)
1207
1208These are misnamed because they don't calculate the Scheme "quotient",
1209but the simple result of flonum {{n1}} divided by the flonum {{n2}},
1210using the storage at {{ptr}}. {{c}} should always be 2.
1211
1212{{C_a_i_flonum_quotient_checked}} will signal an error if {{n2}} is zero.
1213
1214===== C_a_i_flonum_actual_quotient_checked
1215
1216 [C macro] C_word C_a_i_flonum_actual_quotient_checked(C_word **ptr, int c, C_word n1, C_word n2)
1217
1218Due to the misnaming of {{C_a_i_flonum_quotient[_checked]}}, this
1219function has a peculiar name. It calculates the Scheme integer
1220quotient of {{n1}} divided by {{n2}}, using the storage at {{ptr}}.
1221{{c}} should always be 2.
1222
1223If {{n2}} is zero or either of the numbers is not an integral flonum,
1224an error will be signaled.
1225
1226===== C_a_i_flonum_gcd
1227
1228 [C macro] C_word C_a_i_flonum_gcd(C_word **ptr, int c, C_word n1, C_word n2)
1229
1230Calculates the greatest common divisor of the flonums {{n1}} and
1231{{n2}}, using the storage at {{ptr}}. {{c}} should always be 2.
1232
1233===== C_a_i_flonum_negate
1234
1235 [C macro] C_word C_a_i_flonum_negate(C_word **ptr, int c, C_word n)
1236
1237Negates the flonum {{n}}, using the storage at {{ptr}}. {{c}} should
1238always be 1.
1239
1240===== C_a_i_flonum_truncate
1241
1242 [C macro] C_word C_a_i_flonum_truncate(C_word **ptr, int c, C_word n)
1243
1244Truncate the flonum {{n}}, using the storage at {{ptr}}. {{c}} should
1245always be 1.
1246
1247===== C_a_i_flonum_ceiling
1248
1249 [C macro] C_word C_a_i_flonum_ceiling(C_word **ptr, int c, C_word n)
1250
1251Round the flonum {{n}}, rounding upwards, using the storage at
1252{{ptr}}. {{c}} should always be 1.
1253
1254===== C_a_i_flonum_floor
1255
1256 [C macro] C_word C_a_i_flonum_floor(C_word **ptr, int c, C_word n)
1257
1258Round the flonum {{n}}, rounding downwards, using the storage at
1259{{ptr}}. {{c}} should always be 1.
1260
1261===== C_a_i_flonum_round
1262
1263 [C macro] C_word C_a_i_flonum_round(C_word **ptr, int c, C_word n)
1264
1265Round the flonum {{n}}, rounding towards the nearest integer, using
1266the storage at {{ptr}}. {{c}} should always be 1.
1267
1268This macro returns the value like returned by C's {{round()}}
1269function. That means it rounds to the larger value (away from 0) when
1270rounding numbers halfway between two integers.
1271
1272===== C_a_i_flonum_round_proper
1273
1274 [C macro] C_word C_a_i_flonum_round_proper(C_word **ptr, int c, C_word n)
1275
1276Round the flonum {{n}}, rounding towards the nearest integer, using
1277the storage at {{ptr}}. {{c}} should always be 1.
1278
1279This macro returns the value like returned by Scheme's {{round}}
1280procedure. That means it rounds to even numbers when rounding
1281numbers halfway between two integers.
1282
1283===== C_a_i_flonum_sin
1284
1285 [C macro] C_word C_a_i_flonum_sin(C_word **ptr, int c, C_word n)
1286
1287Calculates the sine of {{n}} (in radians).
1288
1289===== C_a_i_flonum_cos
1290
1291 [C macro] C_word C_a_i_flonum_cos(C_word **ptr, int c, C_word n)
1292
1293Calculates the cosine of {{n}} (in radians).
1294
1295===== C_a_i_flonum_tan
1296
1297 [C macro] C_word C_a_i_flonum_tan(C_word **ptr, int c, C_word n)
1298
1299Calculates the tangent of {{n}} (in radians).
1300
1301===== C_a_i_flonum_asin
1302
1303 [C macro] C_word C_a_i_flonum_asin(C_word **ptr, int c, C_word n)
1304
1305Calculates the arc sine of {{n}} (in radians, in the range -pi/2 through +pi/2).
1306
1307===== C_a_i_flonum_acos
1308
1309 [C macro] C_word C_a_i_flonum_acos(C_word **ptr, int c, C_word n)
1310
1311Calculates the arc cosine of {{n}} (in radians, in the range 0 through pi).
1312
1313===== C_a_i_flonum_atan
1314
1315 [C macro] C_word C_a_i_flonum_atan(C_word **ptr, int c, C_word n)
1316
1317Calculates the arc tangent of {{n}} (in radians, in the range -pi/2 through +pi/2).
1318
1319Like C's {{atan()}} or Scheme's unary {{atan}}.
1320
1321===== C_a_i_flonum_atan2
1322
1323 [C macro] C_word C_a_i_flonum_atan2(C_word **ptr, int c, C_word n1, C_word n2)
1324
1325Calculates the arc tangent of {{n1/n2}} (in radians), using the sign of both
1326to determine the quadrant of the result.
1327
1328Like C's {{atan2()}} or Scheme's binary {{atan}}.
1329
1330===== C_a_i_flonum_log
1331
1332 [C macro] C_word C_a_i_flonum_log(C_word **ptr, int c, C_word n)
1333
1334Calculate the natural (base {{e}}) logarithm of {{n}}.
1335
1336===== C_a_i_flonum_exp
1337
1338 [C macro] C_word C_a_i_flonum_exp(C_word **ptr, int c, C_word n)
1339
1340Calculates the base {{e}} exponent of {{n}} (i.e., the inverse
1341operation of {{C_a_i_flonum_log}}).
1342
1343===== C_a_i_flonum_expt
1344
1345 [C macro] C_word C_a_i_flonum_expt(C_word **ptr, int c, C_word n1, C_word n2)
1346
1347Calculates {{n1}} raised to the power {{n2}}.
1348
1349===== C_a_i_flonum_sqrt
1350
1351 [C macro] C_word C_a_i_flonum_sqrt(C_word **ptr, int c, C_word n)
1352
1353Calculates the square root of {{n}}.
1354
1355===== C_a_i_flonum_abs
1356
1357 [C macro] C_word C_a_i_flonum_abs(C_word **ptr, int c, C_word n)
1358
1359Calculates the absolute value of {{n}}.
1360
1361===== C_u_i_flonum_nanp
1362
1363 [C macro] C_word C_u_i_flonum_nanp(C_word n)
1364
1365Is {{n}} a flonum NaN value?
1366
1367===== C_u_i_flonum_finitep
1368
1369 [C macro] C_word C_u_i_flonum_finitep(C_word n)
1370
1371Is {{n}} a finite flonum (i.e., not NaN or one of the infinities)?
1372
1373===== C_u_i_flonum_infinitep
1374
1375 [C macro] C_word C_u_i_flonum_infinitep(C_word n)
1376
1377Is {{n}} an infinite flonum?
1378
1379==== Exact integers
1380
1381Often you know a value is an integer, but you don't know whether it's
1382a fixnum or a bignum. In those cases, there are some optimized C
1383functions and macros to perform operations on them.
1384
1385===== C_i_integer_evenp
1386
1387 [C macro] C_word C_i_integer_evenp(C_word n)
1388
1389Returns {{C_SCHEME_TRUE}} when {{n}} is an even fixnum or bignum,
1390{{C_SCHEME_FALSE}} if it is odd.
1391
1392===== C_i_integer_oddp
1393
1394 [C macro] C_word C_i_integer_oddp(C_word n)
1395
1396Returns {{C_SCHEME_TRUE}} when {{n}} is an odd fixnum or bignum,
1397{{C_SCHEME_FALSE}} if it is even.
1398
1399===== C_i_integer_positivep
1400
1401 [C macro] C_word C_i_integer_positivep(C_word n)
1402
1403Returns {{C_SCHEME_TRUE}} when {{n}} is a positive fixnum or bignum,
1404{{C_SCHEME_FALSE}} if it is zero or negative.
1405
1406===== C_i_integer_negativep
1407
1408 [C macro] C_word C_i_integer_negativep(C_word n)
1409
1410Returns {{C_SCHEME_TRUE}} when {{n}} is a negative fixnum or bignum,
1411{{C_SCHEME_FALSE}} if it is zero or positive.
1412
1413===== C_i_integer_equalp
1414
1415 [C macro] C_word C_i_integer_equalp(x, y)
1416
1417Returns {{C_SCHEME_TRUE}} when {{x}} and {{y}} are numerically equal,
1418{{C_SCHEME_FALSE}} if they differ.
1419
1420===== C_i_integer_greaterp
1421
1422 [C macro] C_word C_i_integer_greaterp(x, y)
1423
1424Returns {{C_SCHEME_TRUE}} when {{x}} is greater than {{y}},
1425{{C_SCHEME_FALSE}} if it is equal or less.
1426
1427===== C_i_integer_greater_or_equalp
1428
1429 [C macro] C_word C_i_integer_greaterp(x, y)
1430
1431Returns {{C_SCHEME_TRUE}} when {{x}} is greater than or equal to {{y}},
1432{{C_SCHEME_FALSE}} if it is less.
1433
1434===== C_i_integer_lessp
1435
1436 [C macro] C_word C_i_integer_lessp(x, y)
1437
1438Returns {{C_SCHEME_TRUE}} when {{x}} is less than {{y}},
1439{{C_SCHEME_FALSE}} if it is equal or greater.
1440
1441===== C_i_integer_less_or_equalp
1442
1443 [C macro] C_word C_i_integer_less_or_equalp(x, y)
1444
1445Returns {{C_SCHEME_TRUE}} when {{x}} is less than or equal to {{y}},
1446{{C_SCHEME_FALSE}} if it is greater.
1447
1448
1449==== Pointers
1450
1451===== C_null_pointerp
1452
1453 [C macro] C_word C_null_pointerp(C_word x)
1454
1455Is {{x}} a NULL pointer?
1456
1457===== C_a_i_address_to_pointer
1458
1459 [C macro] C_word C_a_i_address_to_pointer(C_word **ptr, int c, C_word addr)
1460
1461Convert {{addr}} to a pointer object using the storage at {{ptr}}.
1462{{addr}} is can be either a flonum or a fixnum representing a memory
1463address.
1464
1465===== C_a_i_pointer_to_address
1466
1467 [C macro] C_word C_a_i_pointer_to_address(C_word **ptr, int c, C_word pptr)
1468
1469Convert back the pointer {{pptr}} to an address number, possibly using
1470the storage at {{ptr}}. The number returned can be either a fixnum or
1471a flonum, so you will have to pass a memory storage that can hold a
1472flonum at {{ptr}}. Whether it is actually used depends on the size of
1473the address.
1474
1475
1476==== Ports
1477
1478===== C_tty_portp
1479
1480 [C macro] C_word C_tty_portp(C_word x)
1481
1482Is {{x}} a TTY port object?
1483
1484==== Structures
1485
1486===== C_i_structurep
1487
1488 [C macro] C_word C_i_structurep(C_word x, C_word s)
1489
1490Is {{x}} a structure (record) object with type tag {{s}}? This is
1491completely safe to use, because it checks whether x is an immediate or
1492not.
1493
1494==== Characters
1495
1496These understand only ASCII characters.
1497
1498===== C_u_i_char_alphabeticp
1499
1500 [C macro] C_word C_u_i_char_alphabeticp(C_word c)
1501
1502Is {{c}} an alphabetic character?
1503
1504===== C_u_i_char_numericp
1505
1506 [C macro] C_word C_u_i_char_numericp(C_word c)
1507
1508Is {{c}} a numeric character?
1509
1510===== C_u_i_char_whitespacep
1511
1512 [C macro] C_word C_u_i_char_whitespacep(C_word c)
1513
1514Is {{c}} a whitespace character?
1515
1516===== C_u_i_char_upper_casep
1517
1518 [C macro] C_word C_u_i_char_upper_casep(C_word c)
1519
1520Is {{c}} an uppercase character?
1521
1522===== C_u_i_char_lower_casep
1523
1524 [C macro] C_word C_u_i_char_lower_casep(C_word c)
1525
1526Is {{c}} a lowercase character?
1527
1528
1529=== Other Scheme procedures from C
1530
1531There are a number of Scheme procedures that have a direct C
1532implementation, so you can call them from C too.
1533
1534==== C_eqp
1535
1536 [C macro] C_word C_eqp(C_word a, C_word b)
1537
1538The C version of {{(eq? a b)}}.
1539
1540==== C_equalp
1541
1542 [C macro] C_word C_equalp(C_word a, C_word b)
1543
1544The C version of {{(equal? a b)}}.
1545
1546==== C_i_pairp
1547
1548 [C function] C_word C_i_pairp(C_word x)
1549
1550The C version of {{(pair? x)}}.
1551
1552==== C_i_not_pair_p
1553
1554 [C macro] C_word C_i_not_pair_p(C_word x)
1555
1556The C version of {{(not (pair? x))}}.
1557
1558
1559=== An example for simple calls to foreign code involving callbacks
1560
1561 % cat foo.scm
1562 #>
1563 extern int callout(int, int, int);
1564 <#
1565
1566 (define callout (foreign-safe-lambda int "callout" int int int))
1567
1568 (define-external (callin (scheme-object xyz)) int
1569 (print "This is 'callin': " xyz)
1570 123)
1571
1572 (print (callout 1 2 3))
1573
1574 % cat bar.c
1575 #include <stdio.h>
1576 #include "chicken.h"
1577
1578 extern int callout(int, int, int);
1579 extern int callin(C_word x);
1580
1581 int callout(int x, int y, int z)
1582 {
1583 C_word *ptr = C_alloc(C_SIZEOF_LIST(3));
1584 C_word lst;
1585
1586 printf("This is 'callout': %d, %d, %d\n", x, y, z);
1587 lst = C_list(&ptr, 3, C_fix(x), C_fix(y), C_fix(z));
1588 return callin(lst); /* Note: `callin' will have GC'd the data in `ptr' */
1589 }
1590
1591 % csc foo.scm bar.c -o foo
1592 % foo
1593 This is 'callout': 1, 2, 3
1594 This is 'callin': (1 2 3)
1595 123
1596
1597
1598=== Notes:
1599
1600* Scheme procedures can call C functions, and C functions can call
1601 Scheme procedures, but for every pending C stack frame, the available
1602 size of the first heap generation (the ''nursery'') will be decreased,
1603 because the C stack is identical to the nursery. On systems with a small
1604 nursery this might result in thrashing, since the C code between the
1605 invocation of C from Scheme and the actual calling back to Scheme might
1606 build up several stack-frames or allocates large amounts of stack data.
1607 To prevent this it is advisable to increase the default nursery size,
1608 either when compiling the file (using the {{-nursery}} option)
1609 or when running the executable (using the {{-:s}} runtime option).
1610* Calls to Scheme/C may be nested arbitrarily, and Scheme
1611 continuations can be invoked as usual, but keep in mind that C stack
1612 frames will not be recovered, when a Scheme procedure call from C does
1613 not return normally.
1614* When multiple threads are running concurrently, and control switches
1615 from one thread to another, then the continuation of the current thread
1616 is captured and saved. Any pending C stack frame still active from a
1617 callback will remain on the stack until the threads is re-activated
1618 again. This means that in a multithreading situation, when C callbacks
1619 are involved, the available nursery space can be smaller than expected.
1620 So doing many nested Scheme->C->Scheme calls can reduce the available
1621 memory up to the point of thrashing. It is advisable to have only a
1622 single thread with pending C stack-frames at any given time.
1623* Pointers to Scheme data objects should not be stored in local or
1624 global variables while calling back to Scheme. Any Scheme object not
1625 passed back to Scheme will be reclaimed or moved by the garbage collector.
1626* Calls from C to Scheme are never tail-recursive.
1627* Continuations captured via {{call-with-current-continuation}}
1628 and passed to C code can be invoked like any other Scheme procedure.
1629
1630
1631---
1632Previous: [[Embedding]]
1633
1634Next: [[Data representation]]